import instance from './saxios';

interface HttpConfig {
  url: string;
  data?: any;
  params?: any;
}

interface HttpResponse<T = any> {
  data: T;
  status: number;
  statusText: string;
  headers: any;
  config: HttpConfig;
}

const post = async <T = any>(url: string, data?: any): Promise<T> => {
  try {
    const response = await instance.post<T>(url, data);
    return response.data;
  } catch (error) {
    throw error;
  }
};

const get = async <T = any>(url: string, params?: any): Promise<T> => {
  try {
    const response = await instance.get<T>(url, { params });
    return response.data;
  } catch (error) {
    throw error;
  }
};

const put = async <T = any>(url: string, data?: any): Promise<T> => {
  try {
    const response = await instance.put<T>(url, data);
    return response.data;
  } catch (error) {
    throw error;
  }
};

const del = async <T = any>(url: string, params?: any): Promise<T> => {
  try {
    const response = await instance.delete<T>(url, { params });
    return response.data;
  } catch (error) {
    throw error;
  }
};

export default {
  post,
  get,
  put,
  del,
};
